<template>
  <div class="app-container">
    <el-input
      v-model="filterText"
      placeholder="输入关键字进行过滤"
      style="margin-bottom: 30px"/>

    <el-tree
      ref="tree"
      :data="menuData"
      :props="defaultProps"
      :filter-node-method="filterNode"
      :expand-on-click-node="false"
      show-checkbox
      default-expand-all
      node-key="id"
      highlight-current
      class="filter-tree">
      <span slot-scope="{ node, data }" class="custom-tree-node">
        <span>{{ node.label }}</span>
        <span class="inmiddle">{{ data.url }}</span>
        <!-- <span class="inmiddle">{{ data.api }}</span> -->
        <span>
          <el-button
            class="btn"
            type="text"
            size="mini"
            @click="() => append(data)">
            新增
          </el-button>
          <el-button
            class="btn"
            type="text"
            size="mini"
            @click="() => edit(data)">
            编辑
          </el-button>
          <el-button
            class="btn"
            type="text"
            size="mini"
            @click="() => remove(node, data)">
            删除
          </el-button>
        </span>
      </span>
    </el-tree>

    <el-dialog :title="titleName" :visible.sync="addFormVisible">
      <el-form ref="addForm" :rules="addFormRules" :model="addForm" label-width="80px">
        <el-form-item label="菜单名称" prop="name">
          <el-input v-model="addForm.name" auto-complete="off"/>
        </el-form-item>
        <el-form-item label="vue路由" prop="remark">
          <el-input v-model="addForm.url" auto-complete="off"/>
        </el-form-item>
        <el-form-item label="类型">
          <el-radio v-model="addForm.type" label="1">菜单</el-radio>
          <el-radio v-model="addForm.type" label="2">操作</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addFormVisible = false">取消</el-button>
        <el-button :disabled="formUpLoading" type="primary" @click="submitForm('addForm')">提交</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { menuList, addOrEditMenu, menuDelete } from '../../../api/admin'
export default {
  data() {
    return {
      menuData: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      filterText: '',
      addFormVisible: false,
      titleName: '',
      addFormRules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ]
      },
      // 新增界面数据
      addForm: {
        name: '',
        url: '',
        id: '',
        type: '1'
      },
      formUpLoading: false
    }
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val)
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    getData() {
      menuList().then(data => {
        this.menuData = data.data
      })
    },
    filterNode(value, data) {
      if (!value) return true
      return data.name.indexOf(value) !== -1
    },
    append(data) {
      this.action_row = data
      this.titleName = '新增菜单'
      this.addFormVisible = true
      this.addForm = {
        name: '',
        url: '',
        api: '',
        id: '',
        type: '1',
        pid: data.id,
        level: data.level + 1
      }
    },
    edit(data) {
      console.log(data)
      this.action_row = data
      this.titleName = '编辑菜单'
      this.addFormVisible = true
      this.addForm = {
        name: data.name,
        url: data.url,
        api: data.api,
        id: data.id,
        type: data.type.toString()
      }
    },
    // 创建、编辑提交
    submitForm(formName) {
      this.formUpLoading = true
      addOrEditMenu(this.addForm).then(data => {
        if (data.code === 0) {
          this.$message({ showClose: true, message: '操作成功', type: 'success' })
          this.getData()
          this.addFormVisible = false
        } else {
          this.$message({ showClose: true, message: data.msg, type: 'error' })
        }
        this.formUpLoading = false
      })
    },
    // 删除
    remove(node, data) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        menuDelete({ id: data.id }).then(data => {
          if (data.code === 0) {
            this.getData()
            this.$message({
              showClose: true,
              message: data.data,
              type: 'success'
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style scoped>
  .custom-tree-node {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 14px;
    padding-right: 8px;
  }
  .blocks {
    margin: 60px 0;
  }
  .inmiddle {
    margin-left: 20px;
  }
  .btn {
    float: left;
  }
</style>
